﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Carer.Data;
using Carer.Data.Models;
using System.Web;
using System.Runtime.InteropServices;
using System.Web.Mvc;
using Carer.Crm.Authorize;
using DocumentFormat.OpenXml.Presentation;
using NLog;

namespace Carer.Crm.Controllers
{
    [SessionAuthorize]
    public class BusinessController : BaseController
    {
        private static Logger _logger = LogManager.GetLogger("BusinessController");
        public BusinessController()
        {

        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult AddSchedule(Schedule s, long[] ids)
        {
            if (!s.CrewId.HasValue)
            {
                return base.Json(new SubmitResult(false, "服务人员不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (!s.Start.HasValue)
            {
                return base.Json(new SubmitResult(false, "服务时间不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (!s.End.HasValue)
            {
                return base.Json(new SubmitResult(false, "结束时间不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (!s.Ratio.HasValue)
            {
                return base.Json(new SubmitResult(false, "结算比例不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (ids == null || ids.Length == 0)
            {
                return base.Json(new SubmitResult(false, "请选择至少一个服务订单"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }

            var checkResult = DataService.CheckSchedule(s, ids);
            if (!checkResult.Item1)
            {
                return base.Json(new SubmitResult(false, checkResult.Item2), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }


            s.Operator = base.User.Identity.Name;
            if (DataService.AddSchedule(s, ids))
            {
                return base.Json(new SubmitResult(true, "排班记录保存成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "排班记录保存失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult BonusPage(int page)
        {
            BonusQuery q = base.Session["BonusQuery"] as BonusQuery;
            if (q == null)
            {
                base.Session["BonusQuery"] = q = new BonusQuery();
            }
            q.PageNumber = page;
            return base.Json(DataService.GetBonuses(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult BonusView()
        {
            int? hid = this.GetUserUnitId();
            ViewBag.CrewTypes = DataService.GetAllCrewTypes(hid);
            ViewBag.Categories = DataService.GetAllCategories(hid);
            return base.ViewModel(new List<string>() { "业务管理", "费用管理" });
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult CheckCustomer(Customer c)
        {
            string str;
            if (string.IsNullOrEmpty(c.Name))
            {
                return base.Json(new SubmitResult(false, "客户姓名不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            bool res = DataService.CheckCustomer(c, out str);
            return base.Json(new SubmitResult(res, str), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult CrewPage(int page)
        {
            CrewQuery q = base.Session["CrewQuery"] as CrewQuery;
            if (q == null)
            {
                CrewQuery query2 = new CrewQuery
                {
                    UnitId = this.GetUserUnitId()
                };
                base.Session["CrewQuery"] = q = query2;
            }
            q.PageNumber = page;
            return base.Json(DataService.GetCrews(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult CrewView()
        {
            int? hid = this.GetUserUnitId();
            ViewBag.CrewTypes = DataService.GetAllCrewTypes(hid);
            ViewBag.Categories = DataService.GetAllCategories(hid);
            ViewBag.Payments = DataService.GetAllPayments(hid);
            return base.ViewModel(new List<string>() { "信息维护", "人员管理" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult CustomerPage(int page)
        {
            CustomerQuery q = base.Session["CustomerQuery"] as CustomerQuery;
            if (q == null)
            {
                CustomerQuery query2 = new CustomerQuery
                {
                    UnitId = this.GetUserUnitId()
                };
                base.Session["CustomerQuery"] = q = query2;
            }
            q.PageNumber = page;
            return base.Json(DataService.GetCustomers(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult CustomerView()
        {
            return base.ViewModel(new List<string>() { "信息维护", "客户管理" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteBonus(int id)
        {
            if (DataService.DeleteBonus(id))
            {
                return base.Json(new SubmitResult(true, "费用记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "费用记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteBonuses(int[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条费用记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.DeleteBonus(ids))
            {
                return base.Json(new SubmitResult(true, "费用记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "费用记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteCrew(int id)
        {
            if (DataService.DeleteCrew(id))
            {
                return base.Json(new SubmitResult(true, "人员记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "人员记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteCrews(int[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条人员记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.DeleteCrew(ids))
            {
                return base.Json(new SubmitResult(true, "人员记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "人员记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteCustomer(int id)
        {
            if (DataService.DeleteCustomer(id))
            {
                return base.Json(new SubmitResult(true, "客户记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "客户记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteCustomers(int[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条客户记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.DeleteCustomer(ids))
            {
                return base.Json(new SubmitResult(true, "客户记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "客户记录录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteOrder(long id)
        {
            if (DataService.DeleteOrder(id))
            {
                return base.Json(new SubmitResult(true, "订单记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "订单记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteOrders(long[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条订单记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.DeleteOrder(ids))
            {
                return base.Json(new SubmitResult(true, "订单记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "订单记录录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteSchedule(long id)
        {
            if (DataService.DeleteSchedule(id))
            {
                return base.Json(new SubmitResult(true, "排班记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "排班记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteSchedules(long[] ids)
        {
            if (DataService.DeleteSchedule(ids))
            {
                return base.Json(new SubmitResult(true, "排班记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "排班记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditBonus(Bonus u)
        {
            if (!u.CrewId.HasValue)
            {
                return base.Json(new SubmitResult(false, "服务人员不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (!(u.Date.HasValue && u.Value.HasValue))
            {
                return base.Json(new SubmitResult(false, "调整日期和调整金额不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.EditBonus(u))
            {
                return base.Json(new SubmitResult(true, "费用记录数据保存成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "费用记录数据保存失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditCrew(Crew c, HttpPostedFileBase file)
        {
            string str;
            if ((file != null) && (file.ContentLength > 0))
            {
                byte[] buffer = new byte[file.ContentLength];
                file.InputStream.Read(buffer, 0, file.ContentLength);
                c.PhotoType = file.ContentType;
                c.Photo = buffer;
            }
            if (string.IsNullOrEmpty(c.Name))
            {
                return base.Json(new SubmitResult(false, "人员姓名不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (string.IsNullOrEmpty(c.IdNo))
            {
                return base.Json(new SubmitResult(false, "人员身份证号不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            bool res = DataService.EditCrew(c, out str);
            return base.Json(new SubmitResult(res, str), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditCustomer(Customer u)
        {
            if (DataService.EditCustomer(u))
            {
                return base.Json(new SubmitResult(true, "客户记录保存成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "客户记录保存失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditOrder(Order o)
        {
            if (!(o.CustomerId.HasValue && o.ServiceId.HasValue))
            {
                return base.Json(new SubmitResult(false, "客户和服务项目不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (string.IsNullOrEmpty(o.Area) || string.IsNullOrEmpty(o.Bed))
            {
                return base.Json(new SubmitResult(false, "病区和病床不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }

            if (!o.Start.HasValue)
            {
                return base.Json(new SubmitResult(false, "开始时间不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            else if (o.End.HasValue && o.End.Value <= o.Start.Value)
            {
                return base.Json(new SubmitResult(false, "结束时间必须大于开始时间"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }

            o.UnitId = this.GetUserUnitId();
            if (!o.UnitId.HasValue)
            {
                return base.Json(new SubmitResult(false, "未选择一家单位"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }

            //如果有结束时间，必须小于等于该订单对应的最后一个排班日期
            //新增订单不判断,修改做判断
            if (o.Id > 0 && o.End.HasValue)
            {
                using (CarerEntities entities = new CarerEntities())
                {
                    var filterSchedules = entities.Schedules.Where(it => it.OrderId == o.Id && it.End.HasValue).Select(it => it.End.Value).ToList();
                    if (filterSchedules.Count > 0 && o.End.Value > filterSchedules.Max())
                    {
                        return base.Json(new SubmitResult(false, "结束时间不能大于最后排班结束时间"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
                    }
                }
            }
            o.Addr = String.Format("{0}/{1}", o.Area, o.Bed);
            if (DataService.EditOrder(o))
            {
                return base.Json(new SubmitResult(true, "订单记录保存成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "订单记录保存失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditSchedule(Schedule u)
        {
            string str;
            u.Operator = base.User.Identity.Name;
            bool res = DataService.EditSchedule(u, out str);
            return base.Json(new SubmitResult(res, str), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetBonus(int id)
        {
            return
                base.Json(DataService.GetBonus(id), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetCrew(int id)
        {
            Crew data = DataService.GetCrew(id);
            return base.Json(data, System.Web.Mvc.JsonRequestBehavior.AllowGet, "yyyy-MM-dd");
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.FileResult GetCrewPhoto(int id)
        {
            using (CarerEntities entities = new CarerEntities())
            {
                Crew crew = (from i in entities.Crews
                             where i.Id == id
                             select i).FirstOrDefault<Crew>();
                if ((crew != null) && (crew.Photo != null))
                {
                    return base.File(crew.Photo, crew.PhotoType);
                }
                return base.File(new byte[0], "image/gif");
            }
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetCustomer(int id)
        {
            return
                base.Json(DataService.GetCustomer(id), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetOrder(int id)
        {
            return base.Json(DataService.GetOrder((long)id), System.Web.Mvc.JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetSchedule(long id)
        {
            return base.Json(DataService.GetSchedule(id), System.Web.Mvc.JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }


        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult OrderPage(int page)
        {
            OrderQuery q = base.Session["OrderQuery"] as OrderQuery;
            if (q == null)
            {
                OrderQuery query2 = new OrderQuery
                {
                    UnitId = this.GetUserUnitId()
                };
                base.Session["OrderQuery"] = q = query2;
            }
            q.PageNumber = page;
            return base.Json(DataService.GetOrders(q), System.Web.Mvc.JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        public System.Web.Mvc.ActionResult OrderView()
        {
            int? hid = this.GetUserUnitId();
            ViewBag.CrewTypes = DataService.GetAllCrewTypes(hid);
            ViewBag.Categories = DataService.GetAllCategories(hid);
            ViewBag.Payments = DataService.GetAllPayments(hid);
            return base.ViewModel(new List<string>() { "业务管理", "订单管理" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult PayPage(int page)
        {
            PayQuery q = base.Session["PayQuery"] as PayQuery;
            if (q == null)
            {
                base.Session["PayQuery"] = q = new PayQuery();
            }
            q.PageNumber = page;
            return base.Json(DataService.GetSchedules(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult PayView()
        {
            int? hid = this.GetUserUnitId();
            ViewBag.CrewTypes = DataService.GetAllCrewTypes(hid);
            ViewBag.Categories = DataService.GetAllCategories(hid);
            return base.ViewModel(new List<string>() { "业务管理", "结算管理" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryAvailableOrders(AvailableOrderQuery query)
        {
            return base.Json(DataService.GetAvailableOrder(this.GetUserUnitId(), query), System.Web.Mvc.JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        public System.Web.Mvc.JsonResult QueryAvailableSchedules(ScheduleQuery q)
        {
            q.UnitId = this.GetUserUnitId();
            IList<Schedule> list = DataService.GetSchedulesAll(q);

            return base.Json(list, System.Web.Mvc.JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryBonus(BonusQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["BonusQuery"] as BonusQuery;
                if (q == null)
                {
                    base.Session["BonusQuery"] = q = new BonusQuery();
                }
            }
            else
            {
                base.Session["BonusQuery"] = q;
            }
            return base.Json(DataService.GetBonuses(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryCrew(CrewQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["CrewQuery"] as CrewQuery;
                if (q == null)
                {
                    base.Session["CrewQuery"] = q = new CrewQuery();
                }
            }
            else
            {
                q.UnitId = this.GetUserUnitId();
                base.Session["CrewQuery"] = q;
            }
            return base.Json(DataService.GetCrews(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryCustomer(CustomerQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["CustomerQuery"] as CustomerQuery;
                if (q == null)
                {
                    CustomerQuery query = new CustomerQuery
                    {
                        UnitId = this.GetUserUnitId()
                    };
                    base.Session["CustomerQuery"] = q = query;
                }
            }
            else
            {
                q.UnitId = this.GetUserUnitId();
                base.Session["CustomerQuery"] = q;
            }
            return base.Json(DataService.GetCustomers(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryOrder(OrderQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["OrderQuery"] as OrderQuery;
                if (q == null)
                {
                    OrderQuery query = new OrderQuery
                    {
                        UnitId = this.GetUserUnitId()
                    };
                    base.Session["OrderQuery"] = q = query;
                }
            }
            else
            {
                q.UnitId = this.GetUserUnitId();
                base.Session["OrderQuery"] = q;
            }
            QueryResult<Order> orders = DataService.GetOrders(q);
            return base.Json(orders, System.Web.Mvc.JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryPay(PayQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["PayQuery"] as PayQuery;
                if (q == null)
                {
                    base.Session["PayQuery"] = q = new PayQuery();
                }
            }
            else
            {
                base.Session["PayQuery"] = q;
            }
            q.UnitId = this.GetUserUnitId();
            return base.Json(DataService.GetSchedules(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryPayNew(PayQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["PayQuery"] as PayQuery;
                if (q == null)
                {
                    base.Session["PayQuery"] = q = new PayQuery();
                }
            }
            else
            {
                base.Session["PayQuery"] = q;
            }
            q.UnitId = this.GetUserUnitId();
            return base.Json(DataService.GetSchedulesNew(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryRevenue(RevenueQuery q)
        {
            try
            {
                q.UnitId = this.GetUserUnitId();
                q.State = 0x100;
                return base.Json(DataService.GetOrders(q), System.Web.Mvc.JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "QueryRevenue报错:");
                return base.Json(null, System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryRevenueNew(RevenueQuery q)
        {
            try
            {
                q.UnitId = this.GetUserUnitId();
                q.State = 0x100;
                return base.Json(DataService.GetOrdersNew(q), System.Web.Mvc.JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "QueryRevenueNew报错:");
                return base.Json(null, System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QuerySchedule(ScheduleQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["ScheduleQuery"] as ScheduleQuery;
                if (q == null)
                {
                    base.Session["ScheduleQuery"] = q = new ScheduleQuery();
                }
            }
            else
            {
                base.Session["ScheduleQuery"] = q;
            }
            q.UnitId = this.GetUserUnitId();
            return base.Json(DataService.GetSchedules(q), System.Web.Mvc.JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryWage(WageQuery q)
        {
            q.UnitId = this.GetUserUnitId();
            return base.Json(DataService.GetWages(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryWageNew(WageQuery q)
        {
            q.UnitId = this.GetUserUnitId();
            return base.Json(DataService.GetWagesNew(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryWork(WorkQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["WorkQuery"] as WorkQuery;
                if (q == null)
                {
                    base.Session["WorkQuery"] = q = new WorkQuery();
                }
            }
            else
            {
                base.Session["WorkQuery"] = q;
            }
            q.UnitId = this.GetUserUnitId();
            return base.Json(DataService.GetSchedules(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryWorkNew(WorkQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["WorkQuery"] as WorkQuery;
                if (q == null)
                {
                    base.Session["WorkQuery"] = q = new WorkQuery();
                }
            }
            else
            {
                base.Session["WorkQuery"] = q;
            }
            q.UnitId = this.GetUserUnitId();
            return base.Json(DataService.GetSchedulesNew(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult RevenueView()
        {
            int? hid = this.GetUserUnitId();
            ViewBag.Categories = DataService.GetAllCategories(hid);
            return base.ViewModel(new List<string>() { "业务管理", "收入统计" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult SchedulePage(int page)
        {
            ScheduleQuery q = base.Session["ScheduleQuery"] as ScheduleQuery;
            if (q == null)
            {
                base.Session["ScheduleQuery"] = q = new ScheduleQuery();
            }
            q.PageNumber = page;
            q.UnitId = this.GetUserUnitId();
            return base.Json(DataService.GetSchedules(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult ScheduleView()
        {
            int? hid = this.GetUserUnitId();
            ViewBag.CrewTypes = DataService.GetAllCrewTypes(hid);
            ViewBag.Categories = DataService.GetAllCategories(hid);
            ViewBag.Payments = DataService.GetAllPayments(hid);
            return base.ViewModel(new List<string>() { "业务管理", "排班管理" });
        }

        public System.Web.Mvc.ActionResult WageView()
        {
            int? hid = this.GetUserUnitId();
            ViewBag.CrewTypes = DataService.GetAllCrewTypes(hid);
            ViewBag.Categories = DataService.GetAllCategories(hid);
            return base.ViewModel(new List<string>() { "业务管理", "结算统计" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult WorkPage(int page)
        {
            WorkQuery q = base.Session["WorkQuery"] as WorkQuery;
            if (q == null)
            {
                base.Session["WorkQuery"] = q = new WorkQuery();
            }
            q.PageNumber = page;
            return base.Json(DataService.GetSchedules(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult WorkView()
        {
            int? hid = this.GetUserUnitId();
            ViewBag.CrewTypes = DataService.GetAllCrewTypes(hid);
            ViewBag.Categories = DataService.GetAllCategories(hid);
            return base.ViewModel(new List<string>() { "业务管理", "工量管理" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult SettleOrders(long[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条订单记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.SettleOrders(ids))
            {
                return base.Json(new SubmitResult(true, "订单结账成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "订单结账失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }
    }
}
